package org.enhance.web.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

@Getter
@Setter
@ToString
@ApiModel(value = "返回包装对象")
public class ResponseResult<T extends Object> {
    @ApiModelProperty(value = "响应码")
    private String code;
    @ApiModelProperty(value = "返回数据")
    private T data;
    @ApiModelProperty(value = "异常信息")
    private String msg;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "异常")
    private ErrorDetail error;

    public void addError(String errorCode, String... errorArgs) {
        if (StringUtils.isNotEmpty(errorCode)) {
            error = new ErrorDetail(errorCode, ArrayUtils.isNotEmpty(errorArgs) ? errorArgs : null);
        }
    }

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class ErrorDetail {
        @ApiModelProperty(value = "异常code")
        public String errorCode;
        @JsonInclude(JsonInclude.Include.NON_NULL)
        @ApiModelProperty(value = "异常参数")
        private String[] errorArgs;
    }
}